﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


/// <summary>
/// 宠物召唤管理类
/// </summary>
public class CallUpManager : MonoBehaviour
{
    private bool isExit_Pet = false;
    private int combClickCount = 0;
    private float timer = 2.0f; //计时器
    private bool isOpen = false;
    private GameObject pet;


    public Image processCoverImage;
    public Text combCountText;
    public Text timerText;
    public static CallUpManager Instance;
   
    public Transform petInstantiatePos;

    public GameObject bullPrefab;
    public GameObject magicAreaPrefab;



    private TangoPointCloud m_pointCloud;
    public GameObject flg;
    private bool  hasPutFlag=false;
    private Vector3 planeCenter;
    void Start()
    {
        m_pointCloud = FindObjectOfType<TangoPointCloud>();
    }
    void Awake()
    {
        Instance = this;
    }
    #region

    private float Timer
    {
        get
        {
            return timer;
        }
        set
        {
            timer = value;
            timerText.text = timer.ToString();
        }
    }
    private int CombClickCount
    {
        get
        {
            return combClickCount;
        }
        set
        {
            combClickCount = value;
            combCountText.text = combClickCount.ToString();
            processCoverImage.fillAmount = combClickCount / 10f;
        }
    }
    public bool IsExit_Pet
    {
        get
        {
            return isExit_Pet;
        }
        set
        {
            isExit_Pet = value;
            if (isExit_Pet == true)
            {
                CallUpButtons.Instance.ShowButtons();
            }
            else
            {
                CallUpButtons.Instance.HideButtons();
            }

        }
    }
    #endregion
    /// <summary>
    /// 召唤宠物,召唤按钮点击事件
    /// </summary>
    public void OnCallUpBtnClick()
    {

        if (IsExit_Pet == false)
        {
            CombClickCount++;
            if (CombClickCount > 10)
            {
                CreatMagicArea();
                StartCoroutine(CreatPet());
                isOpen = true;
                IsExit_Pet = true;
                CombClickCount = 0;
                Timer = 0;              
            }
            else
            {
                CallUpButtons.Instance.HideButtons();
                isOpen = false;
            }
        }
    }

    /// <summary>
    /// 计时
    /// </summary>
    void Update()
    {
        if (CombClickCount > 0)
        {
            Timer -= Time.deltaTime;
            if (Timer < 0)
            {
                CombClickCount = 0;
                Timer = 2.0f;
            }
        }
        if (Input.touchCount == 1)
        {
            // Trigger place kitten function when single touch ended.
            Touch t = Input.GetTouch(0);
            if (t.phase == TouchPhase.Ended&& hasPutFlag==false)
            {
                PlaceKitten(t.position);
                hasPutFlag = true;
            }
        }
    }

    void PlaceKitten(Vector2 touchPosition)
    {
        // Find the plane.
        Camera cam = Camera.main;
        
        Plane plane;
        if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane))
        {
            Debug.Log("cannot find plane.");
            return;
        }

        // Place kitten on the surface, and make it always face the camera.
        if (Vector3.Angle(plane.normal, Vector3.up) < 30.0f)
        {
            Vector3 up = plane.normal;
            Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized;
            Vector3 forward = Vector3.Cross(right, plane.normal).normalized;
            Instantiate(flg, planeCenter, Quaternion.LookRotation(forward, up));
        }
        else
        {
            Debug.Log("surface is too steep for kitten to stand on.");
        }
    }


    /// <summary>
    /// 生成宠物
    /// </summary>
    IEnumerator  CreatPet()
    {
        yield return new WaitForSeconds(6f);
        pet = (GameObject)Instantiate(bullPrefab, planeCenter, Quaternion.identity);
        
    }

    /// <summary>
    /// 销毁宠物
    /// </summary>
    public void OnDestroyClick()
    {
        Destroy(pet);
        IsExit_Pet = false;
    }

    /// <summary>
    /// 生成宠物脚下的MagicArea
    /// </summary>
    void CreatMagicArea()
    {
        Instantiate(magicAreaPrefab, planeCenter, Quaternion.identity);
    }
}
